package aestool;

import utils.AESUtils;
import utils.PropertiesUtil;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @version V1.0
 * @Title:
 * @Package
 * @Description:
 * @author: runoob
 * @date:
 */
public class AESTool extends JFrame{

    private JFrame main = new JFrame("AESTool");
    private Container contentPane = main.getContentPane();
    private JTextArea inputArea = new JTextArea(16,63);
    private JTextArea outputArea = new JTextArea(16,63);
    private JPanel inputBar = new JPanel();
    private JPanel btnBar = new JPanel();
    private JPanel dataSecret = new JPanel();
    private JPanel dataSecretIV = new JPanel();
    private JLabel dataSecretLabel = new JLabel("dataSecret:");
    private JLabel dataSecretIVLabel = new JLabel("dataSecretIV:");
    private JTextField dataSecretField = new JTextField(20);
    private JTextField dataSecretIVField = new JTextField(20);
    private JButton encryptBtn = new JButton("加  密");
    private JButton decryptBtn = new JButton("解  密");
    private JLabel outputIcon = new JLabel();
    private AESUtils aesUtils = new AESUtils();

    public PropertiesUtil settings = new PropertiesUtil("SETTINGS.properties");
    Map<String ,String> paramMap = new HashMap<>();

    public AESTool() throws IOException {
        Color bgColor = new Color(67,67,67);
        Font font = new Font("Menu.font", Font.PLAIN, 16);

        contentPane.setLayout(null);
        contentPane.setBounds(0,0,600,800);
        contentPane.setBackground(bgColor);

        dataSecretLabel.setFont(font);
        dataSecretIVLabel.setFont(font);
        dataSecretLabel.setForeground(Color.WHITE);
        dataSecretIVLabel.setForeground(Color.WHITE);
        dataSecret.add(dataSecretLabel);
        dataSecret.add(dataSecretField);
        dataSecretIV.add(dataSecretIVLabel);
        dataSecretIV.add(dataSecretIVField);
        dataSecret.setBackground(bgColor);
        dataSecretIV.setBackground(bgColor);
        dataSecretField.setText(settings.get("dataSecret"));
        dataSecretIVField.setText(settings.get("dataSecretIV"));
        inputBar.add(dataSecret);
        inputBar.add(dataSecretIV);
        inputBar.setLayout(new FlowLayout(FlowLayout.CENTER));
        inputBar.setBounds(7,7,570,45);
        inputBar.setBackground(bgColor);

        outputArea.setEditable(false);
        inputArea.setBounds(7,60,570,300);
        outputArea.setBounds(7,450,570,300);
        inputArea.setBackground(new Color(89,89,89));
        outputArea.setBackground(new Color(89,89,89));
        inputArea.setFont(font);
        outputArea.setFont(font);
        inputArea.setForeground(Color.WHITE);
        outputArea.setForeground(Color.WHITE);

        btnBar.setLayout(null);
        outputIcon.setIcon(new ImageIcon("resources/output.png"));
        outputIcon.setBounds(253,4,64,64);
        encryptBtn.setIcon(new ImageIcon("resources/label.png"));
        encryptBtn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        encryptBtn.setVerticalTextPosition(JLabel.CENTER);
        encryptBtn.setHorizontalTextPosition(JLabel.CENTER);
        encryptBtn.setForeground(Color.WHITE);
        encryptBtn.setBounds(87,15,80,45);
        encryptBtn.addActionListener((e) -> {
            onButtonEncrypt();
        });
        decryptBtn.setIcon(new ImageIcon("resources/label.png"));
        decryptBtn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        decryptBtn.setVerticalTextPosition(JLabel.CENTER);
        decryptBtn.setHorizontalTextPosition(JLabel.CENTER);
        decryptBtn.setForeground(Color.WHITE);
        decryptBtn.setBounds(404,15,80,45);
        decryptBtn.addActionListener((e) -> {
            onButtonDecrypt();
        });

        btnBar.add(encryptBtn);
        btnBar.add(outputIcon);
        btnBar.add(decryptBtn);
//        btnBar.setLayout(new FlowLayout(FlowLayout.CENTER));
        btnBar.setBounds(7,365,570,75);
        btnBar.setBackground(bgColor);

        contentPane.add(inputBar);
        contentPane.add(inputArea);
        contentPane.add(btnBar);
        contentPane.add(outputArea);
        //初始化图形界面
        int screenWidth= java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
        int screenHeight = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;
        main.setSize(600, 800);
        main.setLocation((screenWidth-600)/2, (screenHeight-800)/2);
        main.setLayout(null);
        main.setBackground(Color.GRAY);
        //设置图标
        main.setIconImage(ImageIO.read(new File("resources/logo.png")));
        main.setDefaultCloseOperation(EXIT_ON_CLOSE);
        main.setVisible(true);
        main.repaint();
    }

    public void onButtonEncrypt(){
        aesUtils.dataSecret = dataSecretField.getText();
        aesUtils.dataSecretIV = dataSecretIVField.getText();
        paramMap.put("dataSecret",aesUtils.dataSecret);
        paramMap.put("dataSecretIV",aesUtils.dataSecretIV);
        settings.update(paramMap,"SETTINGS.properties");
        String inputStr = inputArea.getText();
        outputArea.setText(aesUtils.Encrypt(inputStr));
    }

    public void onButtonDecrypt(){
        aesUtils.dataSecret = dataSecretField.getText();
        aesUtils.dataSecretIV = dataSecretIVField.getText();
        paramMap.put("dataSecret",aesUtils.dataSecret);
        paramMap.put("dataSecretIV",aesUtils.dataSecretIV);
        settings.update(paramMap,"SETTINGS.properties");
        String inputStr = inputArea.getText();
        outputArea.setText(aesUtils.Decrypt(inputStr));
    }

}
